﻿using RockBus.Configuration.Interfaces;
using RockBus.PerformanceCounters.Interfaces;
using RockBus.Queueing.Interfaces;
using System;
using System.Threading.Tasks.Dataflow;

namespace RockBus.Queueing.TplDataflow
{
    public class QueueFactory : IQueueFactory
    {
        public QueueFactory(IInternalConfiguration configuration, IPerformanceCounterFactory performanceCounterFactory)
        {
            this.Configuration = configuration;
            this.PerformanceCounterFactory = performanceCounterFactory;
        }

        private IInternalConfiguration Configuration { get; set; }

        private IPerformanceCounterFactory PerformanceCounterFactory { get; set; }

        public IQueue<T> CreateQueue<T>(string name, Action<T> dequeueAction)
        {
            var options = new ExecutionDataflowBlockOptions();
            options.SingleProducerConstrained = false;
            options.MaxDegreeOfParallelism = this.Configuration.MaxDegreeOfParallelism;

            return new ActionBlockQueue<T>(name, dequeueAction, options, this.PerformanceCounterFactory);
        }
    }
}